package marksys.free.com.marksys.ui.user;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import marksys.free.com.marksys.R;
import marksys.free.com.marksys.databinding.ActivityDetailBinding;
import marksys.free.com.marksys.nettool.MyApi;
import marksys.free.com.marksys.nettool.common.Resource;
import marksys.free.com.marksys.nettool.common.Status;
import marksys.free.com.marksys.nettool.vo.Detail;
import marksys.free.com.marksys.ui.SettingActivity;
import marksys.free.com.marksys.ui.adapter.CardPagerViewAdapter;
import marksys.free.com.marksys.ui.adapter.DepthPageTransformer;
import marksys.free.com.marksys.ui.adapter.PhotoEntity;

public class DetailActivity extends AppCompatActivity {
    ActivityDetailBinding binding;
    public static final String RecordID = "RecordID";
    Detail detail;
    long recordId;
    PagerAdapter mAdapter;
    ViewPager mViewPager;
    private ArrayList<PhotoEntity> mData = new ArrayList<>(0);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_detail);
        recordId = getIntent().getLongExtra(RecordID, 0);
        init();
        binding.cancleText.setOnClickListener(v -> this.finish());
        binding.changeBtn.setOnClickListener(v -> {
            Intent intent = new Intent(DetailActivity.this, SettingActivity.class);
            intent.putExtra(SettingActivity.KEY, 2);
            intent.putExtra(SettingActivity.DETAIL, detail);
            startActivityForResult(intent, 0xff);
        });
        binding.deleteBtn.setOnClickListener((View view) -> initDialog());
        mAdapter = new CardPagerViewAdapter(this, mData);
        mViewPager = findViewById(R.id.detail_viewpager);
        //设置Page间间距
        mViewPager.setPageMargin(30);
        //设置缓存的页面数量
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setPageTransformer(false, new DepthPageTransformer());
        mViewPager.setCurrentItem(mAdapter.getCount() / 2);
        mViewPager.setAdapter(mAdapter);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0xff && resultCode == RESULT_OK) {
            setResult(RESULT_OK);
            finish();
        }
    }

    public void init() {
        MyApi.detail(recordId).observe(this, (Resource<Detail> detailResource) -> {
            assert null != detailResource;
            detail = detailResource.data;
            assert detail != null;
            if (detailResource.status == Status.SUCCESS) {
                switch (detail.type) {
                    case 1:
                        binding.includeMenu.detailType.setText("洁");
                        break;
                    case 2:
                        binding.includeMenu.detailType.setText("律");
                        break;
                    case 3:
                        binding.includeMenu.detailType.setText("雅");
                        break;
                    case 4:
                        binding.includeMenu.detailType.setText("勤");
                        break;
                }
                binding.includeMenu.detailItem.setText(detail.projectName);
                binding.includeMenu.detailClass.setText(detail.className);
                binding.includeMenu.detailGrades.setText(String.valueOf(detail.score));
                binding.includeMenu.detailReason.setText(detail.reason);
                binding.textCauseDetail.setText(detail.content);
                if (detail.image != null) {
                    String[] split = detail.image.split(",");
                    displayImage(Arrays.asList(split));
                }

            }
        });
    }

    public void initDialog() {
        new AlertDialog.Builder(DetailActivity.this)
                .setTitle("提示")
                .setMessage("是否删除该评分记录？")
                .setPositiveButton("确定", (dialog, which) -> {
                    //该条评分记录清除
                    MyApi.delete(recordId).observe(this, apiResultResource -> {
                        assert null != apiResultResource;
                        if (apiResultResource.status == Status.SUCCESS) {
                            Toast.makeText(DetailActivity.this, "撤销成功", Toast.LENGTH_SHORT).show();
                            setResult(RESULT_OK);
                            this.finish();
                        }
                    });
                })
                .setNegativeButton("取消", null)
                .show();
    }

    private void displayImage(List<String> imagePath) {
        if (imagePath != null && imagePath.size() > 0) {
            mViewPager.setBackgroundResource(R.color.background);
            for (int i = 0; i < imagePath.size(); i++) {
                mData.add(new PhotoEntity(imagePath.get(i), true));
            }
            mAdapter.notifyDataSetChanged();
        }
    }
}